<?php
/**
 *  APP管理
 *  仲伟涛 2011-4-24
 */

class Pv_Page_Calculate extends Pv_Page_Abstract
{
    public function validate(ME_Request $input, ME_Response $output)
    {
        return true;
    }

    private $db;
    private function loadDb()
    {
        $this->db = Db_Admin::instance();
    }
    public function doDefault(ME_Request $input, ME_Response $output)
    {
        $type = $input->get("type");

        $dataFile = HTML_PATH . "/data/{$type}.data";
        $typeArr = array("document");
        if(!in_array($type,$typeArr)){
            echo "类型错误!";
            exit;
        }
        $this->loadDb();
        $handle = @fopen($dataFile, "r");
        if ($handle) {
            while (!feof($handle)) {
                $buffer = trim(fgets($handle));
                $buffer = preg_replace('/\s{2,}/', ' ', $buffer); //去掉多余的空格
                switch ($type){
                    case "document":
                        $tmpArr = explode(' ', $buffer);
                        if(is_array($tmpArr) && count($tmpArr) == 3){
                            $cnt   = (int)trim($tmpArr[0]);
                            $docId = (int)trim($tmpArr[1]);
                            $appId = (int)trim($tmpArr[2]);
                            if($cnt && $docId && $appId){
                                $sql = "UPDATE cms_document_title_{$appId} SET hits = hits + {$cnt} WHERE id = {$docId}";
                                $this->db->query($sql);
                                //echo $sql;
                            }
                        }
                        break;
                }
            }
            fclose($handle);
        }
        echo "处理完毕!";
        exit;

    }

}
